﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ObjectInstancesPostProcessor.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the ObjectInstancesPostProcessor type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Engine.Factory.Spring
{
    using Collaboris.Utils;

    using global::Spring.Context.Support;
    using global::Spring.Objects.Factory.Config;

    /// <summary>
    /// Registers the object instances againts the dynamic variables subscriptions and publications
    /// </summary>
    public class ObjectInstancesPostProcessor : IObjectPostProcessor
    {
        public delegate void RegisterInstance(string name, object value);

        private readonly RegisterInstance registerInstanceHandler;
        
        /// <summary>
        /// Stores the instance of the spring context to be used by this factory
        /// </summary>
        private XmlApplicationContext springContext;

        public ObjectInstancesPostProcessor(XmlApplicationContext springContext, RegisterInstance registerInstanceHandler)
        {
            ValidationHelper.VerifyObjectArgument(springContext, "springContext");
            ValidationHelper.VerifyObjectArgument(registerInstanceHandler, "registerInstanceHandler");

            this.registerInstanceHandler = registerInstanceHandler;
            this.springContext = springContext;
        }

        #region Implementation of IObjectPostProcessor

        /// <summary>
        /// Apply this <see cref="T:Spring.Objects.Factory.Config.IObjectPostProcessor"/>
        ///  to the given new object instance <i>before</i> any object initialization callbacks.
        /// </summary>
        /// <remarks>
        /// <p>The object will already be populated with property values.
        ///  The returned object instance may be a wrapper around the original.
        /// </p>
        /// </remarks>
        /// <param name="instance">
        /// The new object instance.
        /// </param>
        /// <param name="objectName">
        /// The object Name.
        /// </param>
        /// <returns>
        /// The object instance to use, either the original or a wrapped one.
        /// </returns>
        /// <exception cref="T:Spring.Objects.ObjectsException">
        /// In case of errors.
        /// </exception>
        public object PostProcessBeforeInitialization(object instance, string objectName)
        {
            //IObjectDefinition definition = this.springContext.GetObjectDefinition(objectName);
            registerInstanceHandler(objectName, instance);

            return instance;            
        }

        /// <summary>
        /// Apply this <see cref="T:Spring.Objects.Factory.Config.IObjectPostProcessor"/> to the
        /// given new object instance <i>after</i> any object initialization callbacks.
        /// </summary>
        /// <remarks>
        /// <p>The object will already be populated with property values. The returned object
        ///    instance may be a wrapper around the original.
        /// </p>
        /// </remarks>
        /// <param name="instance">The new object instance.
        /// </param>
        /// <param name="objectName">The name of the object.
        /// </param>
        /// <returns>
        /// The object instance to use, either the original or a wrapped one.
        /// </returns>
        /// <exception cref="T:Spring.Objects.ObjectsException">In case of errors.
        /// </exception>
        public object PostProcessAfterInitialization(object instance, string objectName)
        {
            return instance;
        }

        #endregion
    }
}
